<!--头部公共部分-->
{include file="common/header" /}
<script>
    var course_id = parseInt('{$course_id}'); //课程ID
    var add_status =parseInt('{$add_status}'); //来源
    var campus_id =parseInt('{$campus_id}');
</script>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a><cite>新增班级</cite></a>
      </span>
</div>
<div style="padding:15px;">
    <form class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><span class="x-red">*</span>课程：</label>
                <div class="layui-input-inline">
                        <script>
                            document.write('<select name="course_id" lay-verify="required" lay-search="">');
                            document.write('<option value=""></option>');
                            document.write('{volist name="course" id="v"}');
                            if(add_status == 1){
                                var id = parseInt("{$v.id}");
                                if(course_id == id){
                                    document.write('<option value="{$v.id}" selected>{$v.course_name}</option>');
                                }else{
                                    document.write('<option value="{$v.id}">{$v.course_name}</option>');
                                }
                            }else{
                                document.write('<option value="{$v.id}">{$v.course_name}</option>');
                            }
                            document.write('{/volist}');
                            document.write('</select>');
                        </script>

                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><span class="x-red">*</span>班级名称：</label>
                <div class="layui-input-inline">
                    <input type="text" name="grades_name" lay-verify="required" autocomplete="off" placeholder="请输入班级名称"
                           class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><span class="x-red">*</span>班主任：</label>
                <div class="layui-input-inline">
                    <input type="text" lay-verify="required" name="headmaster" style="cursor: pointer" id="banzhuren" autocomplete="off" readonly
                           placeholder="点击选择班主任" class="layui-input banzhuren">
                    <input type="hidden" id="master_id" name="user_master_id">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">预招人数：</label>
                <div class="layui-input-inline">
                    <input type="number" min="0" name="preoccupation" autocomplete="off" placeholder="不填或输入0表示不限"
                           class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">(人)</div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><span class="x-red">*</span>任课老师：</label>
                <div class="layui-input-inline">
                    <input type="text" lay-verify="required" name="teacher" style="cursor: pointer" id="renkelaoshi" autocomplete="off" readonly
                           placeholder="点击选择老师" class="layui-input renkelaoshi">
                    <input type="hidden" id="teacher_id" name="teacher_id">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">助教：</label>
                <div class="layui-input-inline">
                    <input type="text" name="assistant" style="cursor: pointer" id="zhujiao" autocomplete="off" readonly
                           placeholder="点击选择助教" class="layui-input zhujiao">
                    <input type="hidden" id="assistant_id" name="assistant_id">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><span class="x-red">*</span>计划开班日期：</label>
                <div class="layui-input-inline">
                    <input type="text" name="class_start_date" lay-verify="required" id="class_date" autocomplete="off"
                           placeholder="开班日期" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">计划结业日期</label>
                <div class="layui-input-inline">
                    <input type="text" name="class_end_date"  id="start_date" autocomplete="off"
                           placeholder="不填则按排课次数排满" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">

            <div class="layui-inline">
                <label class="layui-form-label"><span class="x-red">*</span>计划排课：</label>
                <div class="layui-input-inline">
                    <input type="number" min="0" name="project_num" autocomplete="off" lay-verify="required"  placeholder="计划排课次数" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">次</div>
            </div>

            <div class="layui-inline">
                <input type="radio"  lay-filter="repeat_filter" name="repeat_status" value= "1"  title="周" checked>
                <input type="radio" lay-filter="repeat_filter" name="repeat_status" value= "2"  title="日" >
            </div>
            <div class="layui-inline" style="width: 110px">
                <!--
                <select name="interval_value" lay-search="" lay-filter="interval_value">
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                </select>
                -->
                <input type="number" name="interval_value" min="0"  autocomplete="off" value="0" placeholder="间隔时间" class="layui-input">
            </div>
            <div class="layui-inline">
                <div class="layui-form-mid layui-word-aux">(间隔/周-日)</div>
            </div>
        </div>

        <div class="layui-form-item" >
            <label class="layui-form-label">备注：</label>
            <div class="layui-input-inline" style="width: 325px">
                <input type="text" name="remarks" autocomplete="off" class="layui-input">
            </div>
            <input type="checkbox"  value= 1  name="holiday_status" title="是否跳过节假日?" lay-skin="primary" >
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="float: left;"><span class="x-red">*</span>上课时间：</label>
            <table class="layui-table" style="width: 510px;" id="tab">
            <thead>
            <tr>
                <th style="text-align: center;">星期</th>
                <th style="text-align: center;">开始时间</th>
                <th style="text-align: center;">结束时间</th>
                <th style="text-align: center;">教室</th>
                <th style="text-align: center;"><a id="empty" style="cursor: pointer" title="清空"><i class="layui-icon layui-icon-delete"></i></a></th>
            </tr>
            </thead>
                <tbody id="xq">
                <!--<tr>-->
                    <!--<td>星期一</td>-->
                    <!--<td>08:01</td>-->
                    <!--<td>09:01</td>-->
                    <!--<td>204</td>-->
                    <!--<td onclick="classForm_remove(this)"><a style="cursor: pointer" title="移除"><i class="layui-icon layui-icon-delete"></i></a></td>-->
                <!--</tr>-->

                </tbody>
                <tr>
                    <td style="text-align: center;"id="classForm_addTime" class="text-center"><a style="cursor: pointer" class="operation-link">添加...</a></td>
                    <td colspan="4"></td>
                </tr>
            </table>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <input type="hidden" name="course_id" value="{$course_id}">
                <input type="hidden" name="campus_id" value="{$campus_id}">
                <input class="layui-btn layui-btn-normal" lay-submit lay-filter="form" value="立即提交" readonly>

                <script>
                    console.log('course'+course_id)
                    if(add_status == 1){
                        document.write('<a class="layui-btn layui-btn-primary" href="/admin/Grades/grades_course/course_id/'+course_id+'/campus_id/'+campus_id+'">返回</a>');
                    }else{
                        document.write('<a class="layui-btn layui-btn-primary" href="/admin/Grades/lists">返回</a>');
                    }
                </script>
                <!--
                <a class="layui-btn layui-btn-primary" href="{:url('Grades/lists')}">返回</a>
                -->
            </div>
        </div>
    </form>
</div>
<!--底部公共部分-->
{include file="common/footer" /}

<script>

    let arr_checked = [];  //设置初始缓存
    let postion = -1; //数组下标
    function banzhuren(v) {
        console.log(v)
        $(".banzhuren").val(v.field.master_name);
        $("#master_id").val(v.field.master_id);
    }

    function renkelaoshi(v) {
        console.log(v)
        console.log(v.field.teacher)
        $(".renkelaoshi").val(v.field.teacher);
        $("#teacher_id").val(v.field.teacher_id);
    }

    function zhujiao(v) {
        console.log(v)
        console.log(v.field.teacher)
        $(".zhujiao").val(v.field.teacher);
        $("#assistant_id").val(v.field.assistant_id);
    }

    //清楚当前点击的tr
    function classForm_remove(obj)
    {
        var postion = $(obj).find("input[name='pos']").val();
        arr_checked.forEach(function (item, index) {
            if(item.pos == postion){
                arr_checked.remove(index);
            }
        });
        $(obj).closest("tr").remove();
    }

    /**
     *删除数组指定下标或指定对象
     */
    Array.prototype.remove=function(obj){
        for(var i =0;i <this.length;i++){
            var temp = this[i];
            if(!isNaN(obj)){
                temp=i;
            }
            if(temp == obj){
                for(var j = i;j <this.length;j++){
                    this[j]=this[j+1];
                }
                this.length = this.length-1;
            }
        }
    }


    layui.use(['table', 'jquery', 'element', 'form', 'layer', 'laydate'], function () {
        let $ = layui.jquery
            , layer = layui.layer
            , table = layui.table
            , element = layui.element
            , laydate = layui.laydate
            , form = layui.form;
        let campus_id = '{$campus_id}';
        //清楚全部
        $("#empty").click(function() {
            $("#xq").html("");
        });

        $("#classForm_addTime").click(function () {

            var obj = document.getElementsByName("repeat_status");
            var date_obj = document.getElementsByName("class_start_date");
            var value;
            for (var i=0;i<obj.length;i++){ //遍历Radio
                if(obj[i].checked){
                    value=obj[i].value;
                }
            }

            var date_value;
            if(date_obj[0].value.length <= 0){

                date_value = getNowFormatDate();
            }else{
                date_value =date_obj[0].value;
            }
            var url;
            if(value == 1){  //按周
                url = "{:url('Grades/add_time')}?campus_id="+campus_id;

            }else if(value == 2){  //按日
                url = "{:url('Grades/add_days')}?date="+date_value+"&campus_id="+campus_id;
            }

                layer.open({
                type: 2,
                // skin: 'layui-layer-lan',
                closeBtn: 2,
                title: '添加上课时间',
                area: ['90%', '90%'],
                btn:['确认','取消'],
                shadeClose: false,
                content: url,
                yes: function(index, layero){
                    let body = layer.getChildFrame('body', index);
                    let gzr = body.find("input[id='xq']").val();
                    console.log("gzr:"+gzr)
                    if(gzr == ''){
                        layer.msg('请选择上课星期')
                        return;
                    }
                    let time_slot = body.find('select[name=time_slot] option:selected').val();
                    let room_text = body.find('select[name=class_room] option:selected').text();
                    let room_id = body.find('select[name=class_room] option:selected').val();
                    if(room_text == ''){
                        layer.msg('上课教室不能为空')
                        return;
                    }
                    let room = body.find('select[name=class_room] option:selected').val();
                    let start_time_text;
                    let time_long_text;
                    let time_long;
                    let start_time;
                    let end_time
                    if(time_slot == ''){
                        start_time_text = body.find('select[name=start_time] option:selected').text();
                        time_long_text = body.find('select[name=time_long] option:selected').text();
                        time_long = body.find('select[name=time_long] option:selected').val();
                        start_time = body.find('select[name=start_time] option:selected').text()+":00";
                        end_time = body.find("input[name='end_time']").val()+":00";
                        console.log("chenben" +start_time)

                    }else{
                        start_time = body.find('input[name=start_time]').val();
                        time_long = body.find('input[name=time_long]').val();
                        end_time = body.find("input[name='end_time']").val();
                        console.log("chenben" +start_time)
                    }
                    if(start_time == ''){
                        layer.msg('请选择上课开始时间')
                        return;
                    }
                    if(time_long == ''){
                        layer.msg('请选择上课时长')
                        return;
                    }

                    var obj = document.getElementsByName("repeat_status");
                    var res = '';
                    for (var i=0;i<obj.length;i++){ //遍历Radio
                        if(obj[i].checked){
                            if(obj[i].value == 2){
                                $("#class_date").val(gzr)
                                var row = {};
                                row.weekday = gzr;
                                row.start_time = start_time;
                                row.end_time = end_time;
                                row.room_text = room_text;
                                row.room_id = room_id;
                                postion = arr_checked.length + 1;
                                row.pos = arr_checked.length + 1;
                                arr_checked.push(row);

                                var tab=document.getElementById('tab');
                                var rows=tab.rows;
                                var rlen=rows.length //当前的行数
                                if(rlen >= 3){
                                    console.log("row:"+rlen)
                                    tab.deleteRow(1)
                                }
                                var tradd=tab.insertRow(1)
                                tradd.innerHTML='<th style="text-align: center;">'+gzr+'</th><th style="text-align: center;">'+start_time+'</th><th style="text-align: center;">'+end_time+'</th><th style="text-align: center;">'+room_text+'</th> <td onclick="classForm_remove(this)"><input type="hidden" name="pos" value="'+ postion+'"><a style="cursor: pointer" title="移除"><i class="layui-icon layui-icon-delete"></i></a></td>';
                                layer.close(index)
                                return;
                            }

                        }
                    }


                    let gzr_arr = gzr.split(",");
                    console.log(gzr_arr)
                    var res = '';
                    for(let l = 0; l < gzr_arr.length; l++){

                        let day =  gzr_arr[l];  //星期
                        let start_time_ = start_time; //开始时间
                        let end_time_ = end_time; //结束时间
                        let room_text_  = room_text; //教室
                        let room_id_ = room_id; //教室id

                        //校验
                        var objs = eval(arr_checked);
                        var status = false;

                        let now = new Date();
                        let date_basic  = now.getFullYear()+"-"+now.getMonth()+"-"+now.getDate();

                        for(var y = 0;y<objs.length;y++){
                            if(objs[y].weekday === day){  //星期相同默认按周重复
                                if(objs[y].room_text === room_text_){     //教室相同
                                            //判断时间是否重叠
                                            //在开始时间之前

                                            let end_time_basic = new Date(date_basic + ' '+ end_time_);
                                            let start_time_basic = new Date(date_basic + ' '+ objs[y].start_time);
                                            let start_time_basic_ = new Date(date_basic + ' '+ start_time_);
                                            let end_time_basic_ = new Date(date_basic + ' '+ objs[y].end_time);
                                            if(end_time_basic <= start_time_basic){   //结束时间小于开始时间可排


                                                status = false;   //可排


                                            }else if(start_time_basic_ >= end_time_basic_){ //开始时间大于结束时间可排

                                                status = false;   //可排


                                            }else{

                                                status = true;   //不可排
                                                layer.msg(objs[y].weekday+'时间冲突,不可排课')
                                                return;
                                            }



                                            /*
                                            if(start_time_basic_ >= end_time_basic_){ //开始时间大于结束时间可排

                                                status = false;   //可排
                                                continue;

                                            }else{
                                                status = true;   //不可排
                                                layer.msg(objs[y].weekday+'时间冲突,不可排课')
                                                return;
                                            }
                                            */


                                }

                            }

                        }
                        if(!status){
                            var row = {};
                            row.weekday = day;
                            row.start_time = start_time_;
                            row.end_time = end_time_;
                            row.room_text = room_text_;
                            row.room_id = room_id_;
                            postion = arr_checked.length + 1;
                            row.pos = arr_checked.length + 1;
                            arr_checked.push(row);
                        }




                        res += "<tr style='text-align: center;'>";
                        res += "<td>"+day+"</td>";
                        res += "<td>"+start_time_+"</td>";
                        res += "<td>"+end_time_+"</td>";
                        res += "<td>"+room_text_+"</td>";
                        res += "<td style='text-align: center;'onclick='classForm_remove(this)'><input type='hidden' name='pos' value="+ postion+"><a style='cursor: pointer' title='移除'><i class='layui-icon layui-icon-delete'></i></a></td>";
                        res += "</tr>"

                    }

                    /*
                    var objs = eval(arr_checked);
                    for(var m = 0;m<objs.length;m++){
                        console.log("weekday:"+objs[m].weekday);
                    }
                    console.log("json:"+JSON.stringify(arr_checked));
                    */
                    $('#xq').append(res);
                    layer.close(index)


                }
            });
        })

        $("#banzhuren").click(function () {
            layer.open({
                type: 2,
                skin: 'layui-layer-lan',
                closeBtn: 2,
                title: '选择班主任',
                // btn: ['确定', '取消'],
                area: ['90%', '90%'],
                shadeClose: false,
                content: "{:url('Grades/headmaster')}?campus_id="+campus_id,
            });
        })

        $("#renkelaoshi").click(function () {
            layer.open({
                type: 2,
                skin: 'layui-layer-lan',
                closeBtn: 2,
                title: '选择老师',
                // btn: ['确定', '取消'],
                area: ['90%', '90%'],
                shadeClose: false,
                content: "{:url('Grades/teacher')}?campus_id="+campus_id,
            });
        })

        $("#zhujiao").click(function () {
            layer.open({
                type: 2,
                skin: 'layui-layer-lan',
                closeBtn: 2,
                title: '选择助教',
                // btn: ['确定', '取消'],
                area: ['90%', '90%'],
                shadeClose: false,
                content: "{:url('Grades/assistant')}?campus_id="+campus_id,
            });
        })

        //日期
        laydate.render({
            elem: '#class_date',
            type: 'date',
            trigger: 'click',
            value:  getNowFormatDate(),
            done: function(value, date){
                date_min.config.min ={
                    year:date.year,
                    month:date.month-1,
                    date: date.date
                };
            }
        });

        //日期
        var date_min = laydate.render({
            elem: '#start_date',
            min:  getNowFormatDate(),
            type: 'date'
            ,trigger: 'click'
        });

        $('.bbm').show();
        $('.bnj').hide();
        $('.bkm').hide();

        form.on('radio(repeat_filter)', function(data){
            console.log(data);
            arr_checked.splice(0,arr_checked.length);
            if(data.value == 2){
                var tab=document.getElementById('tab');
                var rows=tab.rows;
                var rlen=rows.length //当前的行数
             //   tab.deleteRow(0)
                for(var i = 0;  i< rlen-1; i ++ ){
                    tab.deleteRow(0)
                }
                var tradd=tab.insertRow(0)
                tradd.innerHTML='<th style="text-align: center;">日期</th><th style="text-align: center;">开始时间</th><th style="text-align: center;">结束时间</th><th style="text-align: center;">教室</th> <th style="text-align: center;"><a id="empty" style="cursor: pointer" title="清空"><i class="layui-icon layui-icon-delete"></i></a></th>';

            }  else  if(data.value == 1){
                var tab=document.getElementById('tab');
                var rows=tab.rows;
                var rlen=rows.length //当前的行数
             //   tab.deleteRow(0)
                for(var i = 0;  i< rlen-1; i ++ ){
                    tab.deleteRow(0)
                }
                var tradd=tab.insertRow(0)
                tradd.innerHTML='<th style="text-align: center;">星期</th><th style="text-align: center;">开始时间</th><th style="text-align: center;">结束时间</th><th style="text-align: center;">教室</th> <th style="text-align: center;"><a id="empty" style="cursor: pointer" title="清空"><i class="layui-icon layui-icon-delete"></i></a></th>';

            }
        });

        form.on('radio(b_type)', function (data) {
            var data = data.value;//被点击的radio的value值
            if (data == 1) {
                $('.bbm').show();
                $('.bnj').hide();
                $('.bkm').hide();
            } else if (data == 2) {
                $('.bbm').hide();
                $('.bnj').show();
                $('.bkm').hide();
            } else if (data == 3) {
                $('.bbm').hide();
                $('.bnj').hide();
                $('.bkm').show();
            }
        });

        $('.tbm').show();
        $('.tnj').hide();
        $('.tkm').hide();
        form.on('radio(t_type)', function (data) {
            var data = data.value;//被点击的radio的value值
            if (data == 4) {
                $('.tbm').show();
                $('.tnj').hide();
                $('.tkm').hide();
            } else if (data == 5) {
                $('.tbm').hide();
                $('.tnj').show();
                $('.tkm').hide();
            } else if (data == 6) {
                $('.tbm').hide();
                $('.tnj').hide();
                $('.tkm').show();
            }
        });

        $('.zbm').show();
        $('.znj').hide();
        $('.zkm').hide();
        form.on('radio(z_type)', function (data) {
            var data = data.value;//被点击的radio的value值
            if (data == 7) {
                $('.zbm').show();
                $('.znj').hide();
                $('.zkm').hide();
            } else if (data == 8) {
                $('.zbm').hide();
                $('.znj').show();
                $('.zkm').hide();
            } else if (data == 9) {
                $('.zbm').hide();
                $('.znj').hide();
                $('.zkm').show();
            }
        });

        form.on('select(t_department_id)', function (data) {
            let teacher_department = data.value;
            let ehtml = '';
            $.post('{:url("Teacher/getByDepartment")}', {
                'teacher_department': teacher_department
            }, function (response) {
                let data = response.data;
                console.log(data);
                $.each(data, function (index, element) {
                    ehtml += '<option value="' + element.id + '">' + element.teacher_name +
                        '</option>';
                })
                $('#t_teacher_id').html(ehtml);
                if (data) {
                    $('#t_teacher_id').attr('disabled', false);
                } else {
                    $('#t_teacher_id').attr('disabled', true);
                }
                form.render();
            });
        });

        form.on('select(z_department_id)', function (data) {
            var teacher_department = data.value;
            var ehtml = '';
            $.post('{:url("Teacher/getByDepartment")}', {
                'teacher_department': teacher_department
            }, function (response) {
                var data = response.data;
                console.log(data);
                $.each(data, function (index, element) {
                    ehtml += '<option value="' + element.id + '">' + element.teacher_name +
                        '</option>';
                })
                $('#z_teacher_id').html(ehtml);
                if (data) {
                    $('#z_teacher_id').attr('disabled', false);
                } else {
                    $('#z_teacher_id').attr('disabled', true);
                }
                form.render();
            });
        });

        form.on('select(b_department_id)', function (data) {
            var teacher_department = data.value;
            var ehtml = '';
            $.post('{:url("Teacher/getByDepartment")}', {
                'teacher_department': teacher_department
            }, function (response) {
                var data = response.data;
                console.log(data);
                $.each(data, function (index, element) {
                    ehtml += '<option value="' + element.id + '">' + element.teacher_name +
                        '</option>';
                })
                $('#b_teacher_id').html(ehtml);
                if (data) {
                    $('#b_teacher_id').attr('disabled', false);
                } else {
                    $('#b_teacher_id').attr('disabled', true);
                }
                form.render();
            });
        });


        form.on('select(b_grade_id)', function (data) {
            var teacher_grade = data.value;
            var ehtml = '';
            $.post('{:url("Teacher/getByGrade")}', {
                'teacher_grade': teacher_grade
            }, function (response) {
                var data = response.data;
                console.log(data);
                $.each(data, function (index, element) {
                    ehtml += '<option value="' + element.id + '">' + element.teacher_name +
                        '</option>';
                })
                $('#b_teacher_id').html(ehtml);
                if (data) {
                    $('#b_teacher_id').attr('disabled', false);
                } else {
                    $('#b_teacher_id').attr('disabled', true);
                }
                form.render();
            });
        });

        form.on('select(t_grade_id)', function (data) {
            var teacher_grade = data.value;
            var ehtml = '';
            $.post('{:url("Teacher/getByGrade")}', {
                'teacher_grade': teacher_grade
            }, function (response) {
                var data = response.data;
                console.log(data);
                $.each(data, function (index, element) {
                    ehtml += '<option value="' + element.id + '">' + element.teacher_name +
                        '</option>';
                })
                $('#t_teacher_id').html(ehtml);
                if (data) {
                    $('#t_teacher_id').attr('disabled', false);
                } else {
                    $('#t_teacher_id').attr('disabled', true);
                }
                form.render();
            });
        });

        form.on('select(z_grade_id)', function (data) {
            var teacher_grade = data.value;
            var ehtml = '';
            $.post('{:url("Teacher/getByGrade")}', {
                'teacher_grade': teacher_grade
            }, function (response) {
                var data = response.data;
                console.log(data);
                $.each(data, function (index, element) {
                    ehtml += '<option value="' + element.id + '">' + element.teacher_name +
                        '</option>';
                })
                $('#z_teacher_id').html(ehtml);
                if (data) {
                    $('#z_teacher_id').attr('disabled', false);
                } else {
                    $('#z_teacher_id').attr('disabled', true);
                }
                form.render();
            });
        });

        form.on('select(b_subject_id)', function (data) {
            var teacher_subject = data.value;
            var ehtml = '';
            $.post('{:url("Teacher/getBySubject")}', {
                'teacher_subject': teacher_subject
            }, function (response) {
                var data = response.data;
                console.log(data);
                $.each(data, function (index, element) {
                    ehtml += '<option value="' + element.id + '">' + element.teacher_name +
                        '</option>';
                })
                $('#b_teacher_id').html(ehtml);
                if (data) {
                    $('#b_teacher_id').attr('disabled', false);
                } else {
                    $('#b_teacher_id').attr('disabled', true);
                }
                form.render();
            });
        });

        form.on('select(t_subject_id)', function (data) {
            var teacher_subject = data.value;
            var ehtml = '';
            $.post('{:url("Teacher/getBySubject")}', {
                'teacher_subject': teacher_subject
            }, function (response) {
                var data = response.data;
                console.log(data);
                $.each(data, function (index, element) {
                    ehtml += '<option value="' + element.id + '">' + element.teacher_name +
                        '</option>';
                })
                $('#t_teacher_id').html(ehtml);
                if (data) {
                    $('#t_teacher_id').attr('disabled', false);
                } else {
                    $('#t_teacher_id').attr('disabled', true);
                }
                form.render();
            });
        });

        form.on('select(z_subject_id)', function (data) {
            var teacher_subject = data.value;
            var ehtml = '';
            $.post('{:url("Teacher/getBySubject")}', {
                'teacher_subject': teacher_subject
            }, function (response) {
                var data = response.data;
                console.log(data);
                $.each(data, function (index, element) {
                    ehtml += '<option value="' + element.id + '">' + element.teacher_name +
                        '</option>';
                })
                $('#z_teacher_id').html(ehtml);
                if (data) {
                    $('#z_teacher_id').attr('disabled', false);
                } else {
                    $('#z_teacher_id').attr('disabled', true);
                }
                form.render();
            });
        });



        //监听提交
        form.on('submit(form)', function (data) {
            var field = data.field;
            field.course_id = $('select[name=course_id] option:selected').val();
            console.log(field);
            var campus_id = '{$campus_id}';
            if(field.holiday_status != 1){
                field.holiday_status = 0;
            }
            var classdata = JSON.stringify(arr_checked);
            field.classdata = classdata; //json字符串
            var isLoad;
            $.ajax({
                url: '{:url("Grades/addclassdata")}?status='+2 + "&conflict=" + 2 + "&campus_id=" + campus_id,
                method: "post",
                data: field,
                beforeSend: function () {
                    isLoad = layer.load(0, {
                        shade: false
                    }); //0代表加载的风格，支持0-2
                },
                success: function (result) {
                    console.log("data:"+result.data);
                    if(result.data >= 0){
                      //  layer.msg(result.msg);
                        var title = result.msg;
                        console.log(title)
                        var url;
                        if(result.data === 10001){
                            url = '{:url("Grades/addclassdata")}?status='+ 2 +  "&conflict=" + 3 + "&campus_id=" + campus_id;
                        }else{
                            url = '{:url("Grades/addclassdata")}?status='+ 1  + "&conflict=" + 2 + "&campus_id=" + campus_id;
                        }
                        layer.confirm(title, function (index) {
                            $.ajax({
                                url: url,
                                method: "post",
                                data: field,
                                beforeSend: function () {
                                    isLoad = layer.load(0, {
                                        shade: false
                                    }); //0代表加载的风格，支持0-2
                                },
                                success: function (result) {
                                    if(result.data >= 0){
                                        layer.confirm(result.msg, function (index) {
                                            if(result.data != 10001){
                                                //   location.href = result.url;
                                                url = '{:url("Grades/addclassdata")}?status='+ 1  + "&conflict=" + 3 + "&campus_id=" + campus_id;
                                                $.ajax({
                                                    url: url,
                                                    method: "post",
                                                    data: field,
                                                    beforeSend: function () {
                                                        isLoad = layer.load(0, {
                                                            shade: false
                                                        }); //0代表加载的风格，支持0-2
                                                    },
                                                    success: function (result) {
                                                        layer.msg(result.msg);
                                                        setTimeout(function () {
                                                            if (result.code === 1) {
                                                                if(add_status == 1){
                                                                    location.href = "{:url('grades/grades_course')}?course_id="+course_id+"&campus_id="+campus_id;
                                                                }else{
                                                                    location.href = result.url;
                                                                }

                                                            }
                                                        }, 2000);

                                                    },
                                                    complete: function () {
                                                        layer.close(isLoad)
                                                    },
                                                    error: function (error) {
                                                        console.log(error)
                                                    }
                                                });




                                            }

                                        });
                                        return;
                                    }

                                    layer.msg(result.msg);
                                    setTimeout(function () {
                                        if (result.code === 1) {
                                            if(add_status == 1){
                                                location.href = "{:url('grades/grades_course')}?course_id="+course_id+"&campus_id="+campus_id;
                                            }else{
                                                location.href = result.url;
                                            }
                                        }
                                    }, 2000);

                                },
                                complete: function () {
                                    layer.close(isLoad)
                                },
                                error: function (error) {
                                    console.log(error)
                                }
                            });
                        });
                        return;

                    }
                    layer.msg(result.msg);
                    if (result.code === 1) {
                        if(add_status == 1){
                            location.href = "{:url('grades/grades_course')}?course_id="+course_id+"&campus_id="+campus_id;
                        }else{
                            location.href = result.url;
                        }

                    }
                },
                complete: function () {
                    layer.close(isLoad)
                },
                error: function (error) {
                    console.log(error)
                }
            });
            return false;
        });
    });

    //获取当前时间，格式YYYY-MM-DD
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }
</script>